package com.jbm.jbmsupplier.adapter;

import android.content.Context;
import android.content.Intent;
import android.database.DataSetObserver;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jbm.jbmsupplier.R;
import com.jbm.jbmsupplier.activity.TerMinorActivity;
import com.jbm.jbmsupplier.net.NetIntent;
import com.jbm.jbmsupplier.view.DialogLoading;
import com.jbm.jbmsupplier.view.DoubleButtonDialog;
import com.jbm.onehome.iface.IJOrder;
import com.jbm.onehome.iface.JobOrderEntity;

import java.util.ArrayList;

/**
 * Created by 家帮忙 on 2017/4/11.
 */

public class OrderAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<JobOrderEntity>listorigin;
    private  DialogLoading dialogLoading;
    private final int WCDD = 0002;
    public OrderAdapter(Context context, ArrayList<JobOrderEntity> listorigin) {
        this.context = context;
        this.listorigin = listorigin;
    }

    @Override
    public int getCount() {
        return listorigin.size();
    }

    @Override
    public void registerDataSetObserver(DataSetObserver observer) {
        super.registerDataSetObserver(observer);
    }

    @Override
    public Object getItem(int position) {
        return listorigin.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder=null;
        if (convertView==null){
            convertView=View.inflate(context, R.layout.lz_orderall,null);
            viewHolder=new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else{
            viewHolder=(ViewHolder)convertView.getTag();
        }

        setinitView(viewHolder,listorigin.get(position),position);
        return convertView;
    }
    public void setinitView(ViewHolder viewHolder, final JobOrderEntity order, int p){
//        if (order==null)
//            return;
        if (order.getStatus().equals("new")){
            viewHolder.orderzhuangtai.setText("新订单");
            viewHolder.  textview_title.setText("新订单");
            viewHolder.lianxifangshilayout.setVisibility(View.GONE);
            viewHolder.layout_quxiaoqueren.setVisibility(View.VISIBLE);
        }else if (order.getStatus().equals("confirm")){
            viewHolder.orderzhuangtai.setText("作业中");
            viewHolder.  textview_title.setText("作业中");
        }else if (order.getStatus().equals("submit")){
            viewHolder.orderzhuangtai.setText("待付款");
            viewHolder.  textview_title.setText("待付款");
        }else if (order.getStatus().equals("break")){
            viewHolder.orderzhuangtai.setText("已作废");
            viewHolder.  textview_title.setText("已作废");
        }else if (order.getStatus().equals("revoke")){
            viewHolder.orderzhuangtai.setText("已作废");
            viewHolder.  textview_title.setText("已作废");
        }
        if (order.getIorderEntity()!=null) {
//            if (!viewHolder.orderbianhao.equals("")) {
//                viewHolder.orderbianhao.setText("订单编号:" + order.getIorderEntity().getOrderCode());
//            }
            if (order.getIorderEntity().getStatus().equals("payed")) {

                viewHolder.orderzhuangtai.setText("已付款");
                viewHolder.  textview_title.setText("已付款");
            }
            if (order.getIorderEntity().getStatus().equals("comment")) {

                viewHolder.orderzhuangtai.setText("已评价");
                viewHolder.  textview_title.setText("已评价");
            }
            if (!viewHolder.orderdate.equals("")) {
                viewHolder.orderdate.setText(order.getIorderEntity().getCreateDate() + "");
            }
            if (!viewHolder.orderfunr.equals("")) {
                viewHolder.orderfunr.setText(order.getIorderEntity().getItemDesc());
            }//
            if (!viewHolder.ordername.equals("")) {
                viewHolder.ordername.setText(order.getIorderEntity().getContacts() + "");
            }
            if (!viewHolder.orderdianhau.equals("")) {
                viewHolder.orderdianhau.setText(order.getIorderEntity().getMobile() + "");
            }
            if (!viewHolder.orderdizhi.equals("")) {
                viewHolder.orderdizhi.setText(order.getIorderEntity().getGiText() + "");
            }
        }
//        if (!viewHolder.gdbianhao.equals("")){
//            viewHolder.gdbianhao.setText("工单编号:"+order.getJorderCode());}

        viewHolder.querendigndan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NetIntent netIntent = new NetIntent(handler);
                netIntent.setConfirm(order.getJorderId(), WCDD);
                dialogLoading = new DialogLoading(context, "确认中...", true);
                dialogLoading.show();
            }
        });

viewHolder.zhongzhdigndan_static.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent qintent = new Intent(context, TerMinorActivity.class);
        qintent.putExtra("tag", "2");
        qintent.putExtra("orid", order.getJorderId());
       context. startActivity(qintent);
    }
});




    }
    public  void setNotifin(ArrayList<JobOrderEntity>listorigin){//刷新适配器
        this.listorigin =listorigin;
        notifyDataSetChanged();
    }
private    static class ViewHolder{
TextView orderbianhao,orderzhuangtai,orderdate,orderfunr,orderdianhau,orderdizhi,ordername,gdbianhao,textview_title,querendigndan,zhongzhdigndan_static;
    LinearLayout lianxifangshilayout,layout_quxiaoqueren;
        public ViewHolder(View view) {
            orderbianhao=(TextView)  view.findViewById(R.id.ddbh);
            orderzhuangtai=(TextView) view.findViewById(R.id.stuti);
            orderdate=(TextView) view.findViewById(R.id.ddshij);
            orderfunr=(TextView) view.findViewById(R.id.funr);
            orderdianhau=(TextView) view.findViewById(R.id.dianhua);
            orderdizhi=(TextView) view.findViewById(R.id.dizhi);
            ordername=(TextView) view.findViewById(R.id.tname);
            gdbianhao=(TextView) view.findViewById(R.id.gdbianhao);
            textview_title  =(TextView) view.findViewById(R.id.textview_title);
            lianxifangshilayout=(LinearLayout)view.findViewById(R.id.lianxifangshilayout);
            querendigndan=(TextView)view.findViewById(R.id.querendigndan);
            zhongzhdigndan_static=(TextView)view.findViewById(R.id.zhongzhdigndan_static);
            layout_quxiaoqueren=(LinearLayout)view.findViewById(R.id.layout_quxiaoqueren);
        }
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case WCDD: {
                    IJOrder jor = (IJOrder) msg.obj;
                    if (jor.m_code == 200) {
                      dialogLoading.dismiss();
                    } else if (jor.equals("网络超时")) {
                      hint(jor.m_info);
                        dialogLoading.dismiss();
                    } else {
                        hint(jor.m_info);
                        dialogLoading.dismiss();
                    }
                    break;
                }
            }
        }
    };
    //一些提示的时候调用
    public void hint(String name) {
        final DoubleButtonDialog dialog = new DoubleButtonDialog(context, name);
        dialog.getokbtn().setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.setisVisibility();

    }
}
